package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ProductBean;
import com.project.mapper.IProductMapper;
import com.project.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品业务接口实现类
 */
@Service
@Transactional
public class ProductServiceImpl implements IProductService {
    @Autowired
    private IProductMapper productMapper;
    @Override
    public IPage<ProductBean> findByItem(Integer pageNO, String name, String type) {
        QueryWrapper<ProductBean> qw = new QueryWrapper<>();

        if(name != null && name.length() !=0 ){
            qw.like("p_name",name);
        }
        if(type != null && type.length() !=0 ){
            qw.like("p_type",type);
        }

        return productMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public IPage<ProductBean> findByItemSellPriceNotNull(Integer pageNO, String name, String type) {
        QueryWrapper<ProductBean> qw = new QueryWrapper<>();
        qw.isNotNull("p_sellPrice");

        if(name != null && name.length() !=0 ){
            qw.like("p_name",name);
        }
        if(type != null && type.length() !=0 ){
            qw.like("p_type",type);
        }

        return productMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public void updateSellPrice(String id, Integer sellPrice) {
        ProductBean product = productMapper.selectById(id);
        product.setSellPrice(sellPrice);
        productMapper.updateById(product);
    }

    @Override
    public ProductBean findById(String id) {
        return productMapper.selectById(id);
    }
}
